package app.fugema.shop.main.evaluate.view;

import com.base.common.app.LoginUtils;
import com.base.common.databinding.BaseTitleToolbarBinding;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleUniversalAdapter;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import app.fugema.shop.R;
import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.databinding.ActivityRecyclerviewBinding;
import app.fugema.shop.main.evaluate.model.ProductQuestionBean;
import app.fugema.shop.main.evaluate.model.QusetionResult;
import app.fugema.shop.main.evaluate.vm.EvaluateVM;
import app.fugema.shop.main.login.model.MUserInfo;

public class MyQAActivity extends BaseTitleActivity<ActivityRecyclerviewBinding, EvaluateVM> implements OnGetDataListener {
    //商品评论列表

    public static void start(Context context)
    {
        Intent starter = new Intent(context, MyQAActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    public void initToolBar(BaseTitleToolbarBinding mTitleBinding)
    {
        super.initToolBar(mTitleBinding);
    }

    private SingleUniversalAdapter<ProductQuestionBean> adapter;
    private MUserInfo mUser;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("我的问答",R.color.white);
        adapter = new SingleUniversalAdapter<ProductQuestionBean>()
        {
            @Override
            public int getLayoutId()
            {
                return R.layout.item_my_qa_view;
            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, ProductQuestionBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);
                QuestionDetailActivity.start(view.getContext(), bean.getId());
            }
        };
        binding.rvRecyclerView.setAdapter(adapter);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        if(LoginUtils.isLogin())
        {
            mUser = PublicGlobal.getUserInfo();
        }
        onGetData(1);
    }

    @Override
    public void onGetData(int pn)
    {
        viewModel.getQuestionByUserId(mUser.getUserId(), pn)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<QusetionResult>>(viewUtils.getStateLayout())
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<QusetionResult> data)
                    {

                        viewUtils.setDataListRefreshLayout(adapter, pn, 10, data.data.getData());
                    }

                    @Override
                    public boolean isRefreshLayoutData()
                    {
                        return true;
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<QusetionResult> data)
                    {
                        return  (pn == 1 && UIUtils.isEmpty(data.data.getData()));
                    }
                 });
    }

}

